package net.wofly.right.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.ReturnBean;
import net.wofly.right.domain.User;
import net.wofly.right.dto.RightUserDto;
import net.wofly.right.service.IOrganizationService;
import net.wofly.right.service.IRoleService;
import net.wofly.right.service.IUserService;
import net.wofly.right.validator.UserManageValidator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/api/usermanage")
@Api(description = "公司内部用户管理接口")
public class UserManageController {
	
	private static final String DEFAULT_PAGE_NUM = "1";
    private static final String DEFAULT_PAGE_SIZE = "10";
    
    @Resource
    IUserService iUserService;
    
    @Resource
    IRoleService iRoleService;
    
    @Resource
    IOrganizationService iOrganizationService;
    
	
	@ApiOperation(value = "获取所有公司内部用户")
    @RequestMapping(value = "/findAllUser", method = {RequestMethod.POST} )
	@ApiImplicitParams({
		@ApiImplicitParam(name = "name", value = "用户名称", required = false, dataType ="String", paramType = "form"),
		@ApiImplicitParam(name = "mobile", value = "手机号", required = false, dataType ="String", paramType = "form"),
		@ApiImplicitParam(name = "roleID", value = "角色ID", required = false, dataType ="String", paramType = "form"),
		@ApiImplicitParam(name = "organizationID", value = "组织机构ID", required = false, dataType ="String", paramType = "form"),
		@ApiImplicitParam(name = "pageNum", value = "分页大小", required = false, dataType = "Integer", paramType = "form") ,
		@ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer", paramType = "form")})
    @ResponseBody
    public ReturnBean<PageWrapper<RightUserDto>> findAllUser(String name , String mobile , String roleID , String organizationID,
                                                     @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
                                                     @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) {
        ReturnBean<PageWrapper<RightUserDto>> returnBean;
        String errorMessage = UserManageValidator.validateFindAllUser();
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iUserService.findAllUser(name, mobile, roleID, organizationID, pageNum, pageSize) ;
        }
        return returnBean;
    }
	
	@ApiOperation(value = "新增用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "userName", value = "用户姓名", required = true, dataType ="String", paramType = "form"),
    						@ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType ="String", paramType = "form"),
    						@ApiImplicitParam(name = "organizationID", value = "组织机构ID", required = true, dataType ="String", paramType = "form"),
    						@ApiImplicitParam(name = "roleID", value = "角色ID", required = true, dataType ="String", paramType = "form")})
    @RequestMapping(value = "/saveUser", method = {RequestMethod.POST} )
    @ResponseBody
    public ReturnBean<User> saveUser(HttpServletRequest request, String userName, String mobile ,String organizationID ,String roleID) {
    		ReturnBean<User> returnBean;
        String errorMessage = UserManageValidator.validateSaveUser(userName, mobile , organizationID , roleID) ;
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iUserService.saveUserForUserManage(userName, mobile , organizationID , roleID) ;
        }
        return returnBean;
    }
	
	@ApiOperation(value = "修改用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType ="String", paramType = "form"),
    						@ApiImplicitParam(name = "userName", value = "用户姓名", required = true, dataType ="String", paramType = "form"),
    						@ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType ="String", paramType = "form"),
    						@ApiImplicitParam(name = "organizationID", value = "组织机构ID", required = true, dataType ="String", paramType = "form"),
    						@ApiImplicitParam(name = "roleID", value = "角色ID", required = true, dataType ="String", paramType = "form")})
    @RequestMapping(value = "/updateUser", method = {RequestMethod.POST} )
    @ResponseBody
    public ReturnBean<User> updateUser(HttpServletRequest request, String userID ,String userName, String mobile ,
    											String organizationID ,String roleID) {
    		ReturnBean<User> returnBean;
        String errorMessage = UserManageValidator.validateUpdateUser(userID, userName, mobile , organizationID , roleID) ;
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iUserService.updateUserForUserManage(userID, userName, mobile, organizationID, roleID) ;
        }
        return returnBean;
    }
	
    
	@ApiOperation(value = "禁用用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType ="String", paramType = "form")})
    @RequestMapping(value = "/delUser", method = {RequestMethod.POST} )
    @ResponseBody
    public ReturnBean<String> delUser(HttpServletRequest request, String userID) {
    		ReturnBean<String> returnBean;
        String errorMessage = UserManageValidator.validateUserID(userID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iUserService.delUser(userID) ;
        }
        return returnBean;
    }
	
	@ApiOperation(value = "修改用户手机号")
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType ="String", paramType = "form")})
    @RequestMapping(value = "/updateUserMobile", method = {RequestMethod.POST} )
    @ResponseBody
    public ReturnBean<String> updateUserMobile(HttpServletRequest request, String userID,String mobile) {
    		ReturnBean<String> returnBean;
        String errorMessage = UserManageValidator.validateUserIDAndMobile(userID ,mobile) ;
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iUserService.updateUserMobile(userID,mobile) ;
        }
        return returnBean;
    }

}
